<?php
namespace zake\app\user;


class User
{



    private $_userList = [

        ['id'=>1,'token'=>'11','email'=>'DeCar@gmail.com',  'nick_name'=>'DeCar',   'mobile_phone'=>'18111111111',  'face'=>'./face/b.jpg'],
        ['id'=>2,'token'=>'22','email'=>'AndOne@126.com',   'nick_name'=>'AndOne',  'mobile_phone'=>'1822222222',   'face'=>'./face/a.jpg'],
        ['id'=>3,'token'=>'33','email'=>'xiaoqi@yal.com',   'nick_name'=>'小琪',      'mobile_phone'=>'18333333333',  'face'=>'./face/c.jpg'],
        ['id'=>4,'token'=>'44','email'=>'feifei@gmail.com', 'nick_name'=>'菲菲',      'mobile_phone'=>'18444444444',  'face'=>'./face/d.jpg'],
        ['id'=>5,'token'=>'55','email'=>'schb@163.com',     'nick_name'=>'三尺寒冰',   'mobile_phone'=>'18555555555',  'face'=>'./face/e.jpg'],

    ];



    private $_relation = [

        ['id'=>1,'user_id'=>1,'friends'=>[2,3,4]],
        ['id'=>2,'user_id'=>2,'friends'=>[1,3,4]],
        ['id'=>3,'user_id'=>3,'friends'=>[1,2,5]],
        ['id'=>4,'user_id'=>4,'friends'=>[1,2,5]],
        ['id'=>5,'user_id'=>5,'friends'=>[3,4]],
    ];





    public $id          = 0;
    public $token       = "";
    public $email       = "";
    public $nickName    = "";
    public $mobilePhone = "";
    public $face        = "";
    public $sessionId   = 0;


    /**
     *
     * @param string $token
     *
     * @return User|null
     * @author liu.bin 2020-05-09 15:23
     */
    public static function getUserByToken($token)
    {
        $user = new static();
        foreach ($user->allUser() as $item){
            if($item['token'] === $token){
                $user->userAttr($user,$item);
                return $user;
            }
        }
        return null;
    }


    /**
     * 通过userId查找用户
     *
     * @param $userId
     * @return null|static
     *
     * @author liu.bin 2020-05-09 23:52
     */
    public static function getUserById($userId)
    {
        $user = new static();
        foreach ($user->allUser() as $item){
            if($item['id'] === $userId){
                $user->userAttr($user,$item);
                return $user;
            }
        }
        return null;
    }


    public static function getUserByIds($userIds)
    {
        $useMap = [];
        foreach ($userIds as $userId){
            $user = self::getUserById($userId);
            if($user){
                $useMap[$userId] = $user;
            }
        }
        return $useMap;
    }



    /**
     * 获取我的好友列表
     *
     * @author liu.bin 2020-05-09 23:52
     */
    public function getMyFriend()
    {
        $relationList = $this->_relation;
        foreach ($relationList as $item) {
            if($item['user_id'] == $this->id){
                $userIds = $item['friends'];
                $userMap = User::getUserByIds($userIds);
                return $userMap;
            }
        }
        return [];
    }


    /**
     *
     *
     * @author liu.bin 2020-05-11 10:42
     */
    public function setSessionId($sessionId)
    {
        $this->sessionId = $sessionId;
    }



    public function getSessionId()
    {
        return $this->sessionId;
    }


    /**
     *
     * @return array
     *
     * @author liu.bin 2020-05-09 15:24
     */
    private function allUser()
    {
        return $this->_userList;
    }


    /**
     * 属性赋值
     * @param User $user
     * @param $attrMap
     *
     * @return User
     * @author liu.bin 2020-05-09 23:50
     */
    public function userAttr(User $user,$attrMap)
    {
        $user->id           = $attrMap['id'];
        $user->token        = $attrMap['token'];
        $user->email        = $attrMap['email'];
        $user->nickName     = $attrMap['nick_name']??$attrMap['nickName'];
        $user->mobilePhone  = $attrMap['mobile_phone']??$attrMap['mobilePhone'];
        $user->face         = $attrMap['face'];
        return $user;
    }

}
